from django.conf import settings
from django.conf.urls import url
from django.conf.urls.static import static
from django.urls import path, include, re_path
from django.views.decorators.cache import cache_page
from django.views.static import serve
from MyBlog.my_admin_site import my_site
from MyBlog.rss import LatestBlogFeed
from MyBlog.settings import MEDIA_ROOT
from django.contrib.sitemaps import views as sitemap_views
from MyBlog.sitemap import BlogSitemap

urlpatterns = [
    path(r'admin/', my_site.urls),
    re_path(r'^media/(?P<path>.*)$', serve, {'document_root': MEDIA_ROOT}),  # 访问media文件

    url(r'^ckeditor/', include('ckeditor_uploader.urls')),  # 富文本
    url(r'^rss|feed/', cache_page(60 * 60, key_prefix='rss_cache')(LatestBlogFeed()), name='rss'),  # rss
    url(r'^ $', cache_page(60 * 60, key_prefix='sitemap_cache')(sitemap_views.sitemap),
        {'sitemaps': {'posts': BlogSitemap}}),  # sitemap 使用缓存

    url(r'^learningPlanet/', include(('learningPlanet.urls', 'learningPlanet'), namespace='learningPlanet')),  # 学习星球
    url(r'^playingPlanet/', include(('playingPlanet.urls', 'playingPlanet'), namespace='playingPlanet')),  # 快乐星球
    url(r'^log/', include(('log.urls', 'log'), namespace='log')),  # 请求日志
    url(r'', include(('app.urls', 'app'), namespace='app')),  # 首页

]

if settings.DEBUG:
    import debug_toolbar

    urlpatterns = [
                      url(r'^__debug__/', include(debug_toolbar.urls)),
                  ] + urlpatterns
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
